package com.yhc.mmm;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.yhc.mmm.utils.FragmentFactory;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {
private int currentIndex = 0;
    private FrameLayout frameLayout;
    private RadioGroup raNagivation;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        fragments = new ArrayList<>();
        for (int i = 0; i < raNagivation.getChildCount(); i++) {
            Fragment fragment = FragmentFactory.createFragment(i);
            fragments.add(fragment);
        }
        //默认加载第一个
        getSupportFragmentManager().beginTransaction().add(R.id.fr_nagivation,fragments.get(0)).commit();

        raNagivation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < group.getChildCount(); i++) {
                if (group.getChildAt(i).getId() ==checkedId){
                    getSupportFragmentManager().beginTransaction().replace(R.id.fr_nagivation,fragments.get(i)).commit();
                }
                }
            }
        });
    }

    private void initView() {
        frameLayout = (FrameLayout) findViewById(R.id.fr_nagivation);
        raNagivation = (RadioGroup) findViewById(R.id.rg_nagivation);
    }

}
